package com.unitd.base.module.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.unitd.base.frame.front.SelectOption;
import com.unitd.base.frame.plugins.module.controller.BaseController;
import com.unitd.base.frame.spring.result.AjaxResult;
import com.unitd.base.module.logistics.po.ExpressCompany;
import com.unitd.base.module.logistics.service.ExpressCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 物流公司信息前端处理控制器类
 * @filename ExpressCompanyController.java
 * @copyright www.teewon.com
 * @author hudan
 * @version 1.0
 * @date 2019-01-30
 */
@RestController
@RequestMapping("/expressCompany")
@Slf4j
public class ExpressCompanyController extends BaseController {

	@Autowired
	private ExpressCompanyService expressCompanyService;

	/**
	 * 根据条件获取相应的下拉菜单值
	 * @param reqParam 前端请求参数
	 * @return 物流公司的下来菜单
	 */
	@GetMapping(value = "options")
	public AjaxResult getOptions(@RequestParam Map<String, String> reqParam) {
		/* 组装查询条件 */
		ExpressCompany searchCondition = new ExpressCompany();
		// 快递公司类型(0:常用; 1:国内; 2:国外; 3:转运)
		searchCondition.setType(reqParam.get("type"));
		// 轨迹查询是否支持(0:不支持; 1:支持)
		searchCondition.setSearch(reqParam.get("search"));
		// 数据是否生效(0:失效; 1:生效)
		searchCondition.setState("1");

		// 获取查询结果
		List<ExpressCompany> expressCompanyList = expressCompanyService.listByBean(searchCondition);

		//将查询结果转换成前端需要的数据
		List<SelectOption> expressCompanyDTOList =
				expressCompanyList.stream().map(e -> convertToSelectOption(e)).collect(Collectors.toList());
		log.info("本次下拉菜单信息为: {}", JSON.toJSONString(expressCompanyDTOList));

		return new AjaxResult(expressCompanyDTOList);
	}

	/**
	 * 将数据库对象转换成前端需要的下拉菜单对象对象
	 * @param expressCompany 物流对象
	 * @return 物流DTO对象
	 */
	private SelectOption convertToSelectOption(ExpressCompany expressCompany) {
		SelectOption options = new SelectOption();
		options.setValue(expressCompany.getCode());
		options.setText(expressCompany.getName());
		return options;
	}
}